package com.tianjishuju.cimiss.station.station.manager;

import com.tianjishuju.cimiss.station.station.model.OCF12HForecastVO;
import com.tianjishuju.cimiss.station.station.model.OCF1HForecastVO;
import com.tianjishuju.cimiss.station.station.model.OCF3HForecastVO;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.stereotype.Component;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
public class DownFileRead {

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    public List<Object> readFileOCF1HForecastVO(String fileName) {
        BufferedReader reader = null;
        File file = new File(fileName);
        String row = null;

        List<String> timeStep = Arrays.asList(
                "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10"
                , "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"
                , "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"
                , "31", "32", "33", "34", "35", "36", "37", "38", "39", "40"
                , "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"
                , "51", "52", "53", "54", "55", "56", "57", "58", "59", "60"
                , "61", "62", "63", "64", "65", "66", "67", "68", "69", "70"
                , "71", "72", "75", "78", "81", "84", "87", "90", "93", "96"
                , "99", "102", "105", "108", "111", "114", "117", "120", "123", "126"
                , "129", "132", "135", "138", "141", "144", "147", "150", "153", "156"
                , "159", "162", "165", "168", "180", "192", "204", "216", "228", "240"
                , "252", "264", "276", "288", "300", "312", "324", "336", "348", "360");

        int counter = 1;
        int lineNumber = 4;
        List<Object> rtn = new ArrayList<>();

        String stationID = "";//站号
        String UTC = "";//世界时
        String LST = "";//当地时
        String time_zone = "";//时区
        String report_time_LST = "";//当地发布时间
        String report_time_GMT8 = "";//GMT+8发布时间
        String times_num = "";//预报时效次数；

        String HZ = fileName.substring(fileName.indexOf("GLB_") + 12, fileName.indexOf("GLB_") + 14);//时间频次
        String InsertTime = simpleDateFormat.format(new Date());//写入时间
        String updateFilePath = fileName;//解析的文件名及文件路径

        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"));
            // 一次读入一行，直到读入null为文件结束
            while ((row = reader.readLine()) != null) {//====读取文件的while 循环开始
                //为空行或行数小于3行跳过
                if (counter++ < lineNumber) continue;
                if ("".equals(row)) continue;
                String[] rowValue = row.trim().split(" +");
                String firstValue = rowValue[0];
                if (!timeStep.contains(firstValue)) {
                    stationID = firstValue;
                    UTC = rowValue[4];
                    LST = rowValue[5];
                    time_zone = rowValue[6];
                    report_time_LST = rowValue[7];
                    //report_time_GMT8 = rowValue[8];
                    times_num = rowValue[8];
                } else {
                    if (checkstation(stationID) == 0) {
                        continue;
                    }
                    if (fileName.contains("OCF12H")){
                        OCF12HForecastVO oCF12HForecastVO = getOcf12HForecastVO(InsertTime,HZ, fileName, stationID, UTC, LST, time_zone, report_time_LST, report_time_GMT8, times_num, rowValue);
                        rtn.add(oCF12HForecastVO);
                    }else if (fileName.contains("OCF1H")){
                        OCF1HForecastVO ocf1hForecastVO = getOcf1HForecastVO(stationID, UTC, LST, time_zone, report_time_LST, times_num, HZ, InsertTime, updateFilePath, rowValue);
                        rtn.add(ocf1hForecastVO);
                    }else if (fileName.contains("OCF1H")){
                        OCF3HForecastVO ocf1hForecastVO = getOcf3HForecastVO(InsertTime,stationID, UTC, LST, time_zone, report_time_LST, times_num, HZ, InsertTime, updateFilePath, rowValue);
                        rtn.add(ocf1hForecastVO);
                    }
                }

            }
            return rtn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public OCF3HForecastVO getOcf3HForecastVO(String insertTime,String HZ, String fileName, String stationID, String UTC, String LST, String time_zone, String report_time_LST, String report_time_GMT8, String times_num, String[] rowValue) {
        OCF3HForecastVO ocf3HForecastVO = new OCF3HForecastVO();

        ocf3HForecastVO.setStationID(stationID);//站点号
        ocf3HForecastVO.setUTC(UTC);//世界时
        ocf3HForecastVO.setLST(LST);//当地时
        ocf3HForecastVO.setTime_zone(time_zone);//时区
        ocf3HForecastVO.setReport_time_LST(report_time_LST);//当地发布时间
        ocf3HForecastVO.setReport_time_GMT8(report_time_GMT8);//GMT8发布时间
        ocf3HForecastVO.setTimes_num(times_num);//预报时效次数

        ocf3HForecastVO.setHZ(HZ);//时间频次
        ocf3HForecastVO.setInsertTime(insertTime);//写入时间
        ocf3HForecastVO.setUpdateFilePath(fileName);//解析的文件名及文件路径

        ocf3HForecastVO.setTIME_STEP(rowValue[0]);//预报时效
        ocf3HForecastVO.setUTC_TIME_STEP(rowValue[1]);//与世界时对应的预报时效
        ocf3HForecastVO.setLST_FORECAST_TIME(rowValue[2]);//与当地08时或20时对应的当地时起报时间
        ocf3HForecastVO.setTEMP(rowValue[3]);//时刻温度
        ocf3HForecastVO.setTEMP_MAX(rowValue[4]);//最高温度
        ocf3HForecastVO.setTEMP_MIN(rowValue[5]);//最低温度
        ocf3HForecastVO.setRAIN(rowValue[6]);//降水量
        ocf3HForecastVO.setFF(rowValue[7]);//风速
        ocf3HForecastVO.setFF_LEVEL(rowValue[8]);//风速级别
        ocf3HForecastVO.setFF_LEVEL_DESCRIPTION(get_OCF_FF_LEVEL_DESCRIPTION(rowValue[8]));//风速级别描述
        ocf3HForecastVO.setDD(rowValue[9]);//风向
        ocf3HForecastVO.setDD_LEVEL(rowValue[10]);//风向级别
        ocf3HForecastVO.setDD_LEVEL_DESCRIPTION(get_OCF_DD_LEVEL_DESCRIPTION(rowValue[10]));//风向级别描述
        ocf3HForecastVO.setCLOUD(rowValue[11]);//云量
        ocf3HForecastVO.setWEATHER(rowValue[12]);//天气现象编码
        ocf3HForecastVO.setWEATHER_DESCRIPTION(get_OCF_WEATHER_DESCRIPTION(rowValue[12]));//天气现象描述
        ocf3HForecastVO.setRH(rowValue[13]);//相对温度
        return ocf3HForecastVO;
    }
    public OCF1HForecastVO getOcf1HForecastVO(String stationID, String UTC, String LST, String time_zone, String report_time_LST, String times_num, String HZ, String insertTime, String updateFilePath, String[] rowValue) {
        OCF1HForecastVO ocf1hForecastVO = new OCF1HForecastVO();
        ocf1hForecastVO.setStationID(stationID);
        ocf1hForecastVO.setUTC(UTC);
        ocf1hForecastVO.setLST(LST);
        ocf1hForecastVO.setTime_zone(time_zone);
        ocf1hForecastVO.setReport_time_LST(report_time_LST);
        //ocf1hForecastVO.setReport_time_GMT8(report_time_GMT8);
        ocf1hForecastVO.setTimes_num(times_num);
        ocf1hForecastVO.setHZ(HZ);
        ocf1hForecastVO.setInsertTime(insertTime);
        ocf1hForecastVO.setUpdateFilePath(updateFilePath);
        ocf1hForecastVO.setTIME_STEP(rowValue[0]);//预报时效
        ocf1hForecastVO.setUTC_TIME_STEP(rowValue[1]);//与世界时对应的预报时效
        ocf1hForecastVO.setLST_FORECAST_TIME(rowValue[2]);//与当地08时或20时对应的当地时起报时间
        ocf1hForecastVO.setTEMP(rowValue[3]);//时刻温度
        ocf1hForecastVO.setTEMP_MAX(rowValue[4]);//上一时刻至本时刻内最高温度
        ocf1hForecastVO.setTEMP_MIN(rowValue[5]);//上一时刻至本时刻内最低温度
        ocf1hForecastVO.setRAIN(rowValue[6]);//上一时刻至本时刻的累积降水量
        ocf1hForecastVO.setFF(rowValue[7]);//时刻风速
        ocf1hForecastVO.setFF_LEVEL(rowValue[8]);//时刻风速等级
        ocf1hForecastVO.setFF_LEVEL_DESCRIPTION(get_OCF_FF_LEVEL_DESCRIPTION(rowValue[8]));//时刻风速等级描述
        ocf1hForecastVO.setDD(rowValue[9]);//时刻风向
        ocf1hForecastVO.setDD_LEVEL(rowValue[10]);//时刻风向等级
        ocf1hForecastVO.setDD_LEVEL_DESCRIPTION(get_OCF_DD_LEVEL_DESCRIPTION(rowValue[10]));//时刻风向等级描述
        ocf1hForecastVO.setCLOUD(rowValue[11]);//时刻总云量
        ocf1hForecastVO.setWEATHER(rowValue[12]);//上一时刻至本时刻的最严重的天气现象
        ocf1hForecastVO.setWEATHER_DESCRIPTION(get_OCF_WEATHER_DESCRIPTION(rowValue[12]));//上一时刻至本时刻的最严重的天气现象描述
        ocf1hForecastVO.setRH(rowValue[13]);//时刻相对湿度
        return ocf1hForecastVO;
    }


    public OCF12HForecastVO getOcf12HForecastVO(String insertTime,String HZ, String fileName, String stationID, String UTC, String LST, String time_zone, String report_time_LST, String report_time_GMT8, String times_num, String[] rowValue) {
        OCF12HForecastVO oCF12HForecastVO = new OCF12HForecastVO();

        oCF12HForecastVO.setStationID(stationID);//站点号
        oCF12HForecastVO.setUTC(UTC);//世界时
        oCF12HForecastVO.setLST(LST);//当地时
        oCF12HForecastVO.setTime_zone(time_zone);//时区
        oCF12HForecastVO.setReport_time_LST(report_time_LST);//当地时起报时间
        oCF12HForecastVO.setReport_time_GMT8(report_time_GMT8);//北京时发布时间
        oCF12HForecastVO.setTimes_num(times_num);//预报时效次数；
        oCF12HForecastVO.setHZ(HZ);//时间频次
        oCF12HForecastVO.setInsertTime(insertTime);//写入时间
        oCF12HForecastVO.setUpdateFilePath(fileName);//更新的文件名
        oCF12HForecastVO.setTIME_STEP(rowValue[0]);//预报时效
        oCF12HForecastVO.setLST_FORECAST_TIME(rowValue[1]);//与当地08时或20时对应的当地时起报时间
        oCF12HForecastVO.setTEMP(rowValue[2]);//时刻温度
        oCF12HForecastVO.setTEMP_MAX(rowValue[3]);//最高温度
        oCF12HForecastVO.setTEMP_MIN(rowValue[4]);//最低温度
        oCF12HForecastVO.setRAIN(rowValue[5]);//降水量
        oCF12HForecastVO.setFF(rowValue[6]);//风速
        oCF12HForecastVO.setFF_LEVEL(rowValue[7]);//风速级别
        oCF12HForecastVO.setFF_LEVEL_DESCRIPTION(get_OCF_FF_LEVEL_DESCRIPTION(rowValue[7]));//风速级别描述
        oCF12HForecastVO.setDD(rowValue[8]);//风向
        oCF12HForecastVO.setDD_LEVEL(rowValue[9]);//风向级别
        oCF12HForecastVO.setDD_LEVEL_DESCRIPTION(get_OCF_DD_LEVEL_DESCRIPTION(rowValue[9]));//风向级别描述
        oCF12HForecastVO.setCLOUD(rowValue[10]);//云量
        oCF12HForecastVO.setWEATHER(rowValue[11]);//天气现象编码
        oCF12HForecastVO.setWEATHER_DESCRIPTION(get_OCF_WEATHER_DESCRIPTION(rowValue[11]));//天气现象描述
        oCF12HForecastVO.setRH(rowValue[12]);//相对温度
        oCF12HForecastVO.setRH_MAX(rowValue[13]);//最大相对湿度
        oCF12HForecastVO.setRH_MIN(rowValue[14]);//最小相对湿度
        return oCF12HForecastVO;
    }
    /**
     * get_OCF_FF_LEVEL_DESCRIPTION(根据传入的风速等级，返回风速等级描述)
     *
     * @param FF_LEVEL 风速等级
     * @param @return  FF_LEVEL_DESCRIPTION 风速等级描述
     * @Exception 异常对象
     * @since CodingExample　Ver(编码范例查看) 1.1
     */
    public String get_OCF_FF_LEVEL_DESCRIPTION(String FF_LEVEL) {
        String FF_LEVEL_DESCRIPTION = "";
        switch (FF_LEVEL.substring(0, FF_LEVEL.indexOf("."))) {
            case "0":
                FF_LEVEL_DESCRIPTION = "微风";
                break;

            case "1":
                FF_LEVEL_DESCRIPTION = "3-4级";
                break;

            case "2":
                FF_LEVEL_DESCRIPTION = "4-5级";
                break;

            case "3":
                FF_LEVEL_DESCRIPTION = "5-6级";
                break;

            case "4":
                FF_LEVEL_DESCRIPTION = "6-7级";
                break;

            case "5":
                FF_LEVEL_DESCRIPTION = "7-8级";
                break;

            case "6":
                FF_LEVEL_DESCRIPTION = "8-9级";
                break;

            case "7":
                FF_LEVEL_DESCRIPTION = "9-10级";
                break;

            case "8":
                FF_LEVEL_DESCRIPTION = "10-11级";
                break;

            case "9":
                FF_LEVEL_DESCRIPTION = "11-12级";
                break;
        }
        return FF_LEVEL_DESCRIPTION;
    }

    /**
     * get_OCF_WEATHER_DESCRIPTION(根据传入的天气现象代码，返回天气现象描述)
     *
     * @param WEATHER 风速等级
     * @param @return WEATHER_DESCRIPTION 天气现象描述
     * @Exception 异常对象
     * @since CodingExample　Ver(编码范例查看) 1.1
     */
    public String get_OCF_WEATHER_DESCRIPTION(String WEATHER) {
        String WEATHER_DESCRIPTION = "";
        switch (WEATHER.substring(0, WEATHER.indexOf("."))) {
            case "0":
                WEATHER_DESCRIPTION = "晴";
                break;
            case "1":
                WEATHER_DESCRIPTION = "多云";
                break;
            case "2":
                WEATHER_DESCRIPTION = "阴";
                break;
            case "3":
                WEATHER_DESCRIPTION = "阵雨";
                break;
            case "4":
                WEATHER_DESCRIPTION = "雷阵雨";
                break;
            case "5":
                WEATHER_DESCRIPTION = "雷阵雨伴有冰雹";
                break;
            case "6":
                WEATHER_DESCRIPTION = "雨夹雪";
                break;
            case "7":
                WEATHER_DESCRIPTION = "小雨";
                break;
            case "8":
                WEATHER_DESCRIPTION = "中雨";
                break;
            case "9":
                WEATHER_DESCRIPTION = "大雨";
                break;
            case "10":
                WEATHER_DESCRIPTION = "暴雨";
                break;
            case "11":
                WEATHER_DESCRIPTION = "大暴雨";
                break;
            case "12":
                WEATHER_DESCRIPTION = "特大暴雨";
                break;
            case "13":
                WEATHER_DESCRIPTION = "阵雪";
                break;
            case "14":
                WEATHER_DESCRIPTION = "小雪";
                break;
            case "15":
                WEATHER_DESCRIPTION = "中雪";
                break;
            case "16":
                WEATHER_DESCRIPTION = "大雪";
                break;
            case "17":
                WEATHER_DESCRIPTION = "暴雪";
                break;
            case "18":
                WEATHER_DESCRIPTION = "雾";
                break;
            case "19":
                WEATHER_DESCRIPTION = "冻雨";
                break;
            case "20":
                WEATHER_DESCRIPTION = "沙尘暴";
                break;
            case "21":
                WEATHER_DESCRIPTION = "小到中雨";
                break;
            case "22":
                WEATHER_DESCRIPTION = "中到大雨";
                break;
            case "23":
                WEATHER_DESCRIPTION = "大到暴雨";
                break;
            case "24":
                WEATHER_DESCRIPTION = "暴雨到大暴雨";
                break;
            case "25":
                WEATHER_DESCRIPTION = "大暴雨到特大暴雨";
                break;
            case "26":
                WEATHER_DESCRIPTION = "小到中雪";
                break;
            case "27":
                WEATHER_DESCRIPTION = "中到大雪";
                break;
            case "28":
                WEATHER_DESCRIPTION = "大到暴雪";
                break;
            case "29":
                WEATHER_DESCRIPTION = "浮尘";
                break;
            case "30":
                WEATHER_DESCRIPTION = "扬沙";
                break;
            case "31":
                WEATHER_DESCRIPTION = "强沙尘暴";
                break;
            case "32":
                WEATHER_DESCRIPTION = "浓雾";
                break;
            case "49":
                WEATHER_DESCRIPTION = "强浓雾";
                break;
            case "53":
                WEATHER_DESCRIPTION = "霾";
                break;
            case "54":
                WEATHER_DESCRIPTION = "中度霾";
                break;
            case "55":
                WEATHER_DESCRIPTION = "重度霾";
                break;
            case "56":
                WEATHER_DESCRIPTION = "严重霾";
                break;
            case "57":
                WEATHER_DESCRIPTION = "大雾";
                break;
            case "58":
                WEATHER_DESCRIPTION = "特强浓雾";
                break;
            case "301":
                WEATHER_DESCRIPTION = "雨";
                break;
            case "302":
                WEATHER_DESCRIPTION = "雪";
                break;

        }
        return WEATHER_DESCRIPTION;
    }

    /**
     * get_OCF_DD_LEVEL_DESCRIPTION(根据传入的风速等级，返回风速等级描述)
     *
     * @param DD_LEVEL 风速等级
     * @param @return  DD_LEVEL_DESCRIPTION 风速等级描述
     * @Exception 异常对象
     * @since CodingExample　Ver(编码范例查看) 1.1
     */
    public String get_OCF_DD_LEVEL_DESCRIPTION(String DD_LEVEL) {
        String DD_LEVEL_DESCRIPTION = "";
        switch (DD_LEVEL.substring(0, DD_LEVEL.indexOf("."))) {
            case "0":
                DD_LEVEL_DESCRIPTION = "北风";
                break;

            case "1":
                DD_LEVEL_DESCRIPTION = "东北风";
                break;

            case "2":
                DD_LEVEL_DESCRIPTION = "东风";
                break;

            case "3":
                DD_LEVEL_DESCRIPTION = "东南风";
                break;

            case "4":
                DD_LEVEL_DESCRIPTION = "南风";
                break;

            case "5":
                DD_LEVEL_DESCRIPTION = "西南风";
                break;

            case "6":
                DD_LEVEL_DESCRIPTION = "西风";
                break;

            case "7":
                DD_LEVEL_DESCRIPTION = "西北风";
                break;

            case "8":
                DD_LEVEL_DESCRIPTION = "北风";
                break;
//
//		case "9":
//			DD_LEVEL_DESCRIPTION = "旋转";
//			break;
//
//		default:
//			break;
        }
        return DD_LEVEL_DESCRIPTION;
    }

    public int checkstation(String stationID) {
        int ref = 0;
        List<String> stalist = new ArrayList<String>();
//        stalist = Arrays.asList("63714", "58362", "58238", "57816", "57516", "57494", "57036", "52889", "51463", "51232", "48687", "48647", "42809",
//                "3AJCQ000062", "35188", "27612", "26850", "16716", "16008", "12375", "10400", "10381", "06343");
        stalist = Arrays.asList("36870","27612","12375","17060","10400","42809","16716","57516");

        if (stalist.contains(stationID)) {
            ref = 1;
        }
        return ref;
    }



}
